<template>
  <div id="app">
    <!-- <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">基础数据</el-breadcrumb-item>
     <!-- <el-breadcrumb-item><a href="/">数据管理</a></el-breadcrumb-item>
      <el-breadcrumb-item></el-breadcrumb-item>
      <el-breadcrumb-item>活动详情</el-breadcrumb-item> -->
    <!-- </el-breadcrumb> -->
    <el-form :inline="true" :model="queryfrom" class="demo-form-inline">
      <el-form-item label="类别">
        <el-input v-model="queryfrom.name" placeholder="类别"></el-input>
      </el-form-item>
      <!-- <el-form-item label="条目">
    		<el-input v-model="queryfrom.entry" placeholder="条目"></el-input>
    	</el-form-item>
    	<el-form-item label="值">
    		<el-input v-model="queryfrom.valu" placeholder="值"></el-input>
    	</el-form-item> -->
      <el-form-item>
        <el-button type="primary">帮助</el-button>
        <el-button type="success" @click="dialogFormVisibleadd = true">新建</el-button>
        <el-button type="danger" @click="query('queryfrom')">查询</el-button>
      </el-form-item>
    </el-form>

    <!-- <el-button type="danger" size="mini">帮助</el-button> -->
    <!-- <el-button type="success"  @click="dialogFormVisibleadd = true" size="mini">新建</el-button> -->
    <!-- <el-button type="primary" size="mini">查询</el-button> -->
    <el-dialog title="" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="编号" width="120">
          <el-input v-model="form.id" autocomplete="off" disabled="disabled"></el-input>
        </el-form-item>
        <el-form-item label="条目" width="120">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="是否可编辑" width="10" prop="isDel">
          <template>
            <el-radio v-model="form.isDel" label="0">是</el-radio>
            <el-radio v-model="form.isDel" label="1">否</el-radio>
          </template>
        </el-form-item>
        <!-- <el-form-item label="类别" width="120">
          <el-select v-model="form.region" placeholder="请选择活动区域">
            <el-option label="区域一" value="shanghai"></el-option>
            <el-option label="区域二" value="beijing"></el-option>
          </el-select>
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="edit('form')">确 定</el-button>
      </div>
    </el-dialog>


    <!-- <el-button type="text" @click="dialogTableVisible = true">打开嵌套表格的 Dialog</el-button> -->

    <el-dialog title="详细信息" :visible.sync="dialogTableVisible">
      <el-table :data="queryData">
        <el-table-column property="id" label="编号" width="150"></el-table-column>
        <el-table-column property="name" label="类别" width="200"></el-table-column>
        <el-table-column property="isDel" label="条目" :formatter="stateFormat"></el-table-column>
        <el-table-column fixed="right" label="操作" width="180">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)" v-if="scope.row.isDel==0">
              <span>编辑</span></el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)"
              v-if="scope.row.isDel==0"><span>删除</span></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>


    <el-dialog :visible.sync="dialogFormVisibleadd">
      <el-form :model="addform" :rules="rules" ref="addform">
        <!-- <el-form-item label="编号" width="120">
          <el-input v-model="addform.id" autocomplete="off"></el-input>
        </el-form-item> -->
        <el-form-item label="条目" width="120" prop="name">
          <el-input v-model="addform.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="是否可编辑" width="10" prop="isDel">
          <template>
            <el-radio v-model="addform.isDel" label="0">是</el-radio>
            <el-radio v-model="addform.isDel" label="1">否</el-radio>
          </template>
        </el-form-item>
        <!-- <el-form-item label="类别" width="120">
          <el-select v-model="form.region" placeholder="请选择活动区域">
            <el-option label="区域一" value="shanghai"></el-option>
            <el-option label="区域二" value="beijing"></el-option>
          </el-select>
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisibleadd = false">取 消</el-button>
        <el-button type="primary" @click="addBtn('addform')">确 定</el-button>
      </div>
    </el-dialog>
    <el-table :data="tableData.slice((currentPage-1)*pagesize,currentPage*pagesize)" style="width: 100%">
      <el-table-column prop="id" label="编号" :align="'center'" width="300"></el-table-column>
      <el-table-column prop="name" label="类别" :align="'center'" width="300"></el-table-column>
      <el-table-column prop="isDel" label="是否可编辑" :align="'center'" width="300" :formatter="stateFormat">
      </el-table-column>
      <el-table-column fixed="right" label="操作" :align="'center'" width="180">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)" v-if="scope.row.isDel==0 || juese == 5"><span>编辑</span>
          </el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)" v-if="scope.row.isDel==0 || juese == 5">
            <span>删除</span></el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage"
        :page-sizes="[5, 10, 20, 40]" :page-size="pagesize" layout="total, sizes, prev, pager, next, jumper"
        :total="tableData.length">
      </el-pagination>
    </div>
  </div>
</template>
<script>
  export default {
    data() {
      return {
        juese: '',
        currentPage: 1, //初始页
        pagesize: 5, //    每页的数据
        dialogFormVisible: false,
        queryData: [],
        addform: {
          id: '',
          name: '',
          isDel: '0'
        },
        dialogTableVisible: false,
        queryfrom: {
          name: ''
        },
        form: {
          id: '',
          name: '',
          isDel: ''
        },
        rules: {
          name: [{ required: true, message: '请输入条目', trigger: 'blur' }],
        },
        dialogFormVisibleadd: false,
        tableData: [], //表格初始化
        // depts: [],
      }
    },
    methods: {
      findAll() {
          //查询所有
          this.$http.get("/basic/findAll").then(res => {
            this.tableData = res.data;
          });
      },
      handleSizeChange(size) {
        this.pagesize = size;
      },
      handleCurrentChange(currentPage) {
        this.currentPage = currentPage;
      },
      addBtn(addform) {
        this.$refs[addform].validate(valid => {
          if (valid) {
            this.$http.post("/basic/add", this.addform).then(res => {
              if (res.data.code === 0) {
                this.findAll();
                this.dialogFormVisibleadd = false
                this.$message({
                  type: 'success',
                  message: res.data.msg
                })
              } else {
                this.$message({
                  type: 'error',
                  message: res.data.msg
                })
              }
            }).catch(err => {
              this.dialogFormVisibleadd = false
              this.$message.error('添加失败，请稍后再试！')
            })
            this.addform = {isDel: '0'};
          } else {
            return false
          }
        })
      },
      stateFormat: function (row, column) {
        if (row.isDel == 0) {
          return '是'
        } else {
          return '否'
        }
      },
      edit(form) {
        let _this = this;
        this.$http.post("/basic/updateById", _this.form).then(res => {
          this.dialogFormVisible = false;
          this.findAll();
          return;
        });
      },
      query() {
        let _this = this;
        console.log(this.queryfrom);
        this.$http.post("/basic/query", _this.queryfrom).then(res => {
          this.dialogTableVisible = true;
          this.queryData = res.data;
          return;
        });
      },
      handleDelete($index, row) {
        this.$confirm('此操作将永久删除, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
          center: true
        }).then(() => {
          this.$http.post("/basic/delid/" + row.id).then(res => {
            this.findAll();
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      //显示编辑界面
      handleEdit: function (index, row) {
        this.dialogFormVisible = true
        if (row != undefined && row != 'undefined') {
          this.title = '修改'
          this.form.id = row.id
          this.form.name = row.name
          this.form.isDel = row.isDel
        } else {
          this.title = '添加'
          this.form.id = ''
          this.form.name = ''
          this.form.isDel = ''
        }
      },

    },
    created() {
      this.findAll();
      this.juese = localStorage.getItem("nId");
    }
  }
</script>

<style>
  /* .el-pagination {
  margin-top: 10px;
  float: right;
}
.el-dialog__header {
  background-color: #409eff;
}
.el-dialog__title {
  color: white;
} */
</style>